package com.boyi.cycling_master.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.boyi.cycling_master.R;
import com.boyi.cycling_master.bean.LoginResponseBean;
import com.boyi.cycling_master.common.SnackUtil;
import com.boyi.cycling_master.common.TextCheckedUtil;
import com.boyi.cycling_master.https.utils.UserRetrofit;
import com.boyi.cycling_master.ui.base.BaseActivity;
import com.rengwuxian.materialedittext.MaterialEditText;

import org.w3c.dom.Text;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import talon.com.underlineText.UnderLineTextView;

/**
 * @author Lee_Mofeel
 * @version 1.0
 * @date 2017/7/19/019 15:34
 * @Description 登录
 */
public class PhoneLoginActivity extends BaseActivity {


    @BindView(R.id.btn_nav_icon)
    ImageView btnNavIcon;
    @BindView(R.id.edt_user_phone)
    MaterialEditText edtUserPhone;
    @BindView(R.id.edt_user_password)
    MaterialEditText edtUserPassword;
    @BindView(R.id.tv_btn_to_login)
    TextView tvBtnToLogin;
    @BindView(R.id.tv_btn_to_login_xtx)
    TextView tvBtnToLoginXtx;
    @BindView(R.id.tv_btn_forget_pwd)
    TextView tvBtnForgetPwd;
    @BindView(R.id.iv_login_with_ali)
    ImageView ivLoginWithAli;
    @BindView(R.id.iv_login_with_qq)
    ImageView ivLoginWithQq;
    @BindView(R.id.iv_login_with_wx)
    ImageView ivLoginWithWx;
    @BindView(R.id.tv_underline)
    UnderLineTextView tvUnderline;
    @BindView(R.id.tv_login_text)
    TextView tvLoginText;
    @BindView(R.id.tv_login_hint_phone)
    TextView tvLoginHintPhone;
    private boolean isPhoneLogin = true;

    @Override
    public int getContentViewId() {
        return R.layout.activity_phone_login;
    }


    @Override
    public void initLogic() {

    }

    @Override
    public void getBundleExtras(Bundle bundle) {

    }


    @OnClick({R.id.btn_nav_icon, R.id.tv_btn_to_login, R.id.tv_btn_to_login_xtx, R.id.tv_btn_forget_pwd, R.id.iv_login_with_ali, R.id.iv_login_with_qq, R.id.iv_login_with_wx, R.id.tv_underline})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_nav_icon:// 返回
                finish();
                break;
            case R.id.tv_btn_to_login:// 登录
                login();
                break;
            case R.id.tv_btn_to_login_xtx:// 登录方式切换
                isPhoneLogin = !isPhoneLogin;
                if (isPhoneLogin) {// 电话号码登录
                    tvBtnToLoginXtx.setText("行天下账号登录");
                    tvLoginText.setText("登录");
                    tvLoginHintPhone.setText("手机号");
                    edtUserPhone.setHint("请输入您的手机号");
                } else {// 行天下账号登录
                    tvBtnToLoginXtx.setText("手机号登录");
                    tvLoginText.setText("行天下登录");
                    tvLoginHintPhone.setText("行天下账号");
                    edtUserPhone.setHint("请输入行天下账号");
                }
                break;
            case R.id.tv_btn_forget_pwd:// 忘记密码
                startActivity(new Intent(this, RetrievePwdActivity.class));
                break;
            case R.id.iv_login_with_ali:// 第三方 阿里
                break;
            case R.id.iv_login_with_qq:// 第三方 qq
                break;
            case R.id.iv_login_with_wx:// 第三方 微信
                break;
            case R.id.tv_underline:// 没有账号
                startActivity(new Intent(this, RegisterPhoneActivity.class));
                break;
        }
    }

    /**
     * 登录
     */
    public void login() {
        String userName = edtUserPhone.getText().toString();
        String password = edtUserPassword.getText().toString();
        if (userName != null && password != null) {
            Map<String, String> map = new HashMap<>();
            if (isPhoneLogin) {// 根据登录方式的不一样 传递不一样的参数到服务器
                if (TextCheckedUtil.checkPhoneNum(userName))
                    map.put("userPhone", userName);
                else {
                    SnackUtil.SnackShort(edtUserPassword, "请输入11位手机号");
                    return;
                }
            } else
                map.put("userName", userName);
            map.put("userPassword", password);
            Log.d("requestData", map.toString());
            // 执行网络请求
            UserRetrofit.getInstance().toLogin(new Callback<LoginResponseBean>() {
                @Override
                public void onResponse(Call<LoginResponseBean> call, Response<LoginResponseBean> response) {
                    LoginResponseBean loginResponseBean = response.body();
                    if (loginResponseBean == null)
                        return;
                    SnackUtil.SnackShort(edtUserPassword, loginResponseBean.getResultMessage());
                    if (loginResponseBean.getResultCode().equals("200")) {// 登录成功
                        loginResponseBean.getDataCollection().save();// 保存到本地中
                        startActivity(new Intent(PhoneLoginActivity.this, PersonCenterActivity.class));// 跳转到下一个界面
                    }
                }

                @Override
                public void onFailure(Call<LoginResponseBean> call, Throwable t) {
                    SnackUtil.SnackShort(edtUserPassword, "服务器异常，请稍后重试");
                }
            }, map);
        } else {
            SnackUtil.SnackShort(edtUserPassword, "请输入相关信息");
        }
    }


}


